//
//  CheckSecurityViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/21.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "CheckSecurityViewController.h"
#import "ModifyNumViewController.h"
#import "FindWordViewController.h"
#import "SetPayWordViewController.h"
@interface CheckSecurityViewController ()<UITextFieldDelegate>

@property(nonatomic ,strong) UITextField *passWord1;
@property(nonatomic ,strong) UIButton *getBtn;
@property(nonatomic ,strong) UIButton *MySureBtn;
@property(nonatomic ,strong) NSString  *phone;

@end

@implementation CheckSecurityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=MyString(@"安全检测");
    [self creatUI];
    self.phone=[NSString stringWithFormat:@"%@",[ZJUserDefaultsTools getCurrentUserPhone]];
    [self GetNumeberBtnClick];
    // Do any additional setup after loading the view.
}

-(void)creatUI{
    MYLabel*label=[[MYLabel alloc]init];
    label.text=MyString(@"我们已经发送了短信验证码到您的手机:");
    label.textColor=TextColor6;
    label.font=FontAndStyle12;
    label.backgroundColor=[UIColor clearColor];
    label.textAlignment=1;
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(20);
        make.right.left.equalTo(0);
        make.height.equalTo(15);
    }];
    NSString*string =[NSString stringWithFormat:@"%@", [ZJUserDefaultsTools getCurrentUserPhone]];
    string = [string substringWithRange:NSMakeRange(7, string.length-7)];//
    MYLabel*numlabel=[[MYLabel alloc]init];
    numlabel.text=[NSString stringWithFormat:@"********%@", string];
    numlabel.textColor=TextColor3;
    numlabel.backgroundColor=[UIColor clearColor];
    numlabel.textAlignment=1;
    [self.view addSubview:numlabel];
    [numlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(10);
        make.right.left.equalTo(0);
        make.height.equalTo(15);
    }];
    CGFloat width=MainW-25;
    self.passWord1=[[UITextField alloc]init];
    self.passWord1.delegate=self;
    self.passWord1.font=FontAndStyle14;
    self.passWord1.textColor=TextColor3;
    self.passWord1.backgroundColor=[UIColor whiteColor];
    self.passWord1.placeholder=MyString(@"请输入短信验证码验证码");
    [self.passWord1 setValue:FontAndStyle14 forKeyPath:@"_placeholderLabel.font"];
    [self.passWord1 setValue:TextColor9 forKeyPath:@"_placeholderLabel.textColor"];
    self.passWord1.keyboardType=UIKeyboardTypeNumberPad;
    self.passWord1.borderStyle=0;
    [self.view addSubview:self.passWord1];
    ViewRadius(self.passWord1, 3);
    [self.passWord1 addTarget:self action:@selector(controlTextDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.passWord1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(numlabel.mas_bottom).offset(20);
        make.left.equalTo(10);
        make.height.equalTo(49);
        make.width.equalTo(width*2/3);
    }];
    self.getBtn=[[UIButton alloc]init];
    [self.getBtn setTitle:MyString(@"获取验证码") forState:UIControlStateNormal];
    [self.getBtn setBackgroundColor:[UIColor whiteColor]];
    [self.getBtn setTitleColor:Red_Color forState:UIControlStateNormal];
    self.getBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [self.getBtn addTarget:self action:@selector(getBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.getBtn];
    ViewRadius(self.getBtn, 3);
    [self.getBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(numlabel.mas_bottom).offset(20);
        make.right.equalTo(-10);
        make.height.equalTo(49);
        make.width.equalTo(width/3);
    }];
    MYLabel*pour=[[MYLabel alloc]init];
    pour.text=MyString(@"如无法自助修改,请联系客服");
    pour.textColor=TextColor9;
    pour.font=FontAndStyle12;
    pour.backgroundColor=[UIColor clearColor];
    pour.textAlignment=0;
    NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc] initWithString:pour.text attributes:nil];
    [attributedString setAttributes:@{NSForegroundColorAttributeName:Red_Color} range:NSMakeRange(11, 2)];
    pour.attributedText=attributedString;
    [self.view addSubview:pour];
    [pour mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.getBtn.mas_bottom).offset(10);
        make.left.equalTo(10);
        make.height.equalTo(15);
        make.width.equalTo(MainW-20);
    }];
    UIButton *btn=[[UIButton alloc]init];
    [btn addTarget:self action:@selector(contactBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.getBtn.mas_bottom).offset(10);
        make.left.equalTo(10);
        make.height.equalTo(15);
        make.width.equalTo(MainW-20);
    }];
    self.MySureBtn=[[UIButton alloc]init];
    [self.MySureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.MySureBtn setTitle:MyString(@"下一步") forState:UIControlStateNormal];
    [self.MySureBtn addTarget:self action:@selector(NextSureClick) forControlEvents:UIControlEventTouchUpInside];
    [self.MySureBtn setBackgroundColor:Red_Color];
    [self.view addSubview:self.MySureBtn];
    ViewRadius(self.MySureBtn, 5);
    [self.MySureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(pour.mas_bottom).offset(20);
        make.left.equalTo(10);
        make.height.equalTo(49);
        make.width.equalTo(MainW-20);
    }];
}
-(void)contactBtnClick{
    //NSLog(@"联系客服");
}
-(void)NextSureClick{
        if (self.passWord1.text.length==0) {
            [self showToast:MyString(@"请输入验证码")];
        }else{
            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
            dic[@"phone"]=self.phone;
            dic[@"smsCode"]=self.passWord1.text;
            [self doHttpRequestWithApi:API_CHECK_SMS method:GET dic:dic successBlock:^(NSDictionary *dic) {
                if ([dic[@"status"] intValue]==0) {
                    if (self.styple==1) {
                        ModifyNumViewController*vc=[[ModifyNumViewController alloc]init];
                        [self.navigationController pushViewController:vc animated:YES];
                    }else{
                        SetPayWordViewController *vc=[[SetPayWordViewController alloc]init];
                        if (self.styple==2) {
                            vc.statue=2;
                            vc.type=2;
                        }else{
                            vc.statue=3;
                        }
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                    
                    
                }else{
                    [self showToast:dic[@"msg"]];
                }
                
            }];
            
        }
   
    //NSLog(@"下一步");
}
-(void)getBtnClick{
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    
    [parametersDic setObject:self.phone forKey:@"phone"];
    
    [self doHttpRequestWithApi:API_SEND_SMS method:POST dic:parametersDic successBlock:^(NSDictionary *dic) {
        if ([dic[@"status"] intValue]==0) {
            [self showMessage:MyString(@"验证码发送成功，请查收") ];
            [self startTime];
        }else{
           [self showMessage:dic[@"msg"]];
        }
        
        //[self startTime];
    }];
   
}
- (void)startTime
{
    __block int timeout=30; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout <= 0)
        { //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                self.getBtn.titleLabel.text=MyString(@"重新获取");
               // self.passWord1.enabled=NO;
                self.getBtn.userInteractionEnabled = YES;
            });
        }
        else
        {
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //self.passWord1.enabled=YES;
                [self.getBtn setTitle:[NSString stringWithFormat:@"%@%@",strTime,MyString(@"秒后重试")] forState:UIControlStateNormal];
                self.getBtn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

-(void)controlTextDidChange:(NSNotification *)obj {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [self performSelector:@selector(nizana) withObject:obj afterDelay:3.0];
}
-(void)nizana{
    //NSLog(@"监听到了吗");
    [self.passWord1 endEditing:YES];
}
-(void)GetNumeberBtnClick{
    //NSLog(@"获取验证码");
    if (self.phone.length!=0) {
        if (![ZJUITools checkTel:self.phone]) {
            [self showToast:MyString(@"请输入正确的手机号码")];
            return;
        }
        NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
        
        [parametersDic setObject:self.phone forKey:@"phone"];
        
        [self doHttpRequestWithApi:API_SEND_SMS method:POST dic:parametersDic successBlock:^(NSDictionary *dic) {
            if ([dic[@"status"] intValue]==0) {
                [self showMessage:MyString(@"验证码发送成功，请查收") ];
                [self startTime];
            }else{
                [self showMessage:dic[@"msg"]];
            }
            //[self showMessage:@"验证码发送成功，请查收" ];
        }];
        
    }else{
        [self showToast:MyString(@"手机号码不能为空")];
    }
    
}

@end
